/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fei.ClassDiagram;

import org.fei.ClassDiagram.Compiler.AntCompilator;
import org.fei.ClassDiagram.WindowComponents.Message;
import org.fei.ClassDiagram.WindowComponents.MessageJPanel;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import org.fei.ClassDiagram.ClassFileScanner;
import org.fei.ClassDiagram.ProjectPropertiesScanner;
import org.fei.ClassDiagram.ProjectScanner;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;

/**
 * Trieda implementuje rozhranie Runnable. Prekryva metodu run, ktora sa spusta v oddelenom vlakne.
 * Toto vsetko je koli tomu aby nezamrzalo GUI pri generovani diagramov.
 * 
 * @author Tomas
 */
public class RunAction implements Runnable {

    private AbstractAction actionClass;
    private Project p;
    private ProjectScanner ps;

    public RunAction(AbstractAction actionClass, Project p) {
        this.actionClass = actionClass;
        this.p = p;
        this.ps = ProjectScannerFactory.getProjectScanner();
    }

    @Override
    public void run() {
        // najprv sa zavre posledny tab
        if (ClassDiagramView.lastOpened != null)
            ClassDiagramView.lastOpened.close();
        
        if (AntCompilator.run(ps.getBuildImplXml()) == 0) {
                String[] messages = {"Error during compilation.", "Please check your project on syntax errors and try again."};
                new ClassDiagramView(new MessageJPanel(messages), ps.getProjectName());
                actionClass.setEnabled(true);
                GenerateDiagramProjectPopupAction.setEnabling(true);
                return;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Message.showMessage("RunAction.java:" + ex.toString());
        }
        // ziskanie adresara v ktorom su class subory
        ProjectPropertiesScanner propertiesScanner = new ProjectPropertiesScanner(ps.getProjectProperties().getPath());
        FileObject classesDir = p.getProjectDirectory().getFileObject(propertiesScanner.getClassesDir());

        ArrayList<String> classFiles = new ArrayList<String>();
        ps.findFiles(classesDir, "class", classFiles);

        ClassFileScanner classScanner = new ClassFileScanner(classFiles, classesDir.getPath());
        actionClass.setEnabled(true);
        GenerateDiagramProjectPopupAction.setEnabling(true);
    }
}
